package com.xiaojie.summer.resolver;

/**
 * @author Nick
 * @date 2021/5/23
 * @description
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Nick
 * @date 2021/5/23
 * @description 类解析器
 */
public class ClassResolver {
    /**
     * 解析包的全路径
     *
     * @param webPackage
     * @return
     */
    public String resolvePackagePath(String webPackage) {
        // 根据包名拿到路径
        File f = new File(getClass().getResource("/").getPath());
        String totalPath = f.getAbsolutePath();
        String pageName = getClass().getPackage().getName().replace(".", "\\");
        totalPath = totalPath.replace(pageName, "");
        String packagePath = webPackage.replace(".", "\\");
        totalPath = totalPath + "\\" + packagePath;
        return totalPath;
    }

    /**
     * 解析出所有的Class列表
     *
     * @param packagePath
     * @param webPackage
     * @return
     */
    public List<String> parseClassName(String packagePath, String webPackage) {
        List<String> array = new ArrayList<>();
        File root = new File(packagePath);
        resolveFile(root, webPackage, array);
        return array;
    }

    /**
     * @param root       基本路径
     * @param webPackage 包名
     * @param classNames 用于存类名的列表
     */
    private void resolveFile(File root, String webPackage, List<String> classNames) {
        if (!root.exists()) {
            return;
        }
        File[] childList = root.listFiles();
        if (childList != null && childList.length > 0) {
            for (File child : childList) {
                if (child.isDirectory()) {
                    String parentPath = child.getParent();
                    String childPath = child.getAbsolutePath();
                    String temp = childPath.replace(parentPath, "");
                    temp = temp.replace("\\", "");
                    resolveFile(child, webPackage + "." + temp, classNames);
                } else {
                    String fileName = child.getName();
                    if (fileName.endsWith(".class")) {
                        String name = fileName.replace(".class", "");
                        String className = webPackage + "." + name;
                        classNames.add(className);
                    }
                }
            }
        }
    }


    /**
     * 根据顶部类加载下面所有的类
     *
     * @param topClass
     * @return
     */
    public List<Class> loadAllClassListOnTopClass(Class topClass) throws ClassNotFoundException {

        List<Class> classList = new ArrayList<>();

        String packageName = topClass.getPackage().getName();
        ClassResolver classResolver = new ClassResolver();
        // 拿到包路径
        String packagePath = classResolver.resolvePackagePath(packageName);
        // 拿到类名列表
        List<String> classNameList = classResolver.parseClassName(packagePath, packageName);
        for (String className : classNameList) {
            Class theClass = Class.forName(className);
            classList.add(theClass);
        }

        return classList;
    }
}
